﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using SimpleRest.Core.Routing;
using SimpleRest.Core.Routing.Handlers;
using SimpleRest.Core;
using SimpleRest.Core.Extensions;
using System.Web.Routing;
using SimpleRest.Core.Extensions.OAuth;
using SimpleRest.WebTest.Services;

namespace SimpleRest.WebTest
{
    // uncomment this class and comment out the other global class to see how auto discovery works.
    public class Global : HttpApplicationBase // notice the new class that global inherits from.  This is simply a helper class to assist in driving functionality.
    {
        public override void Initialize(object obj, ApplicationExecutionEventArgs e)
        {
           // RouteUtility.IsNativeRouteRegistrationEnabled = false;
            // This is where you set configuration's for the services you are about to register.  Simply creating a new configuration object
            // will not have XSD support nor will it have default XML and JSON media type formatters registered.  If you do not want to use the
            // extensions to create your configuration then you will have to register the media type formatters.
            var configuration = ConfigurationFactory.CreateConfiguration();
            
            configuration.ErrorHandler = args =>
            {
                args.ResponseMessage.Content = args.Exception.ToString();
            };
            
            configuration.ApplyOAuthWorkflow();
            //configuration.ApplyOAuthWorkflow<AuthorizationService>();
            //configuration.ApplyOAuthWorkflow(new AuthorizationService());
            // ******************************* Different ways to register services in SimpleRest *******************************

            // scan assembilies and auto register all services using a single configuration object.
         //   e.ScanAndRegisterAssembilies(configuration, System.Reflection.Assembly.GetExecutingAssembly());
            configuration.CreateServiceInstance = (service, request) =>
            {
                return System.Activator.CreateInstance(service);
            };

            configuration.ExtendedOptions.IsDefaultJsonMediaTypeFormatted = true;
            configuration.ExtendedOptions.IsDefaultXmlMediaTypeFormatted = true;
            configuration.ExtendedOptions.IsXsdFormatted = true;

            ServiceDiscoveryFactory.ScanAndRegisterServices(configuration, System.Reflection.Assembly.GetExecutingAssembly());
            // manually register the services via the event args
            //e.RegisterService(typeof(Services.ReviewsService), configuration);
            //e.RegisterService(typeof(Services.ExceptionService), configuration);
            //e.RegisterService(typeof(Services.ProductsService), configuration);


            // manually register services not using event args
            //ServiceDiscoveryFactory.RegisterService(typeof(Services.ReviewsService), configuration);
            //ServiceDiscoveryFactory.RegisterService(typeof(Services.ExceptionService), configuration);
            //ServiceDiscoveryFactory.RegisterService(typeof(Services.ProductsService), configuration);


            // manually register services using RouteTable
            //ServiceRoute<Services.ProductsService> productsServiceRoute = new ServiceRoute<Services.ProductsService>(configuration);
            //ServiceRoute<Services.ReviewsService> reviewsServiceRoute = new ServiceRoute<Services.ReviewsService>(configuration);
            //ServiceRoute<Services.ExceptionService> exceptionsServiceRoute = new ServiceRoute<Services.ExceptionService>(configuration);
            //RouteUtility.Routes.Add(productsServiceRoute);
            //RouteUtility.Routes.Add(reviewsServiceRoute);
            //RouteUtility.Routes.Add(exceptionsServiceRoute);
        }

        public void Application_BeginRequest()
        {
        }
    }
}
